/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Image;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.core.ui.dialogs.PapyrusMessageDialog;

/**
 * 
 */
public class StereotypeImageSection extends PropertyViewSection {

	/**
	 * 
	 */
	private Button browseButton, removeButton;
	
	/**
	 * 
	 */
	private MouseListener browseButtonListener, removeButtonListener;
	
	/**
	 * 
	 */
	private String sectionLabel = "Shape:";

	/**
	 * 
	 */
	private final org.eclipse.swt.graphics.Image remove_img 
	= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"Delete_16x16.gif");
	
	/**
	 * 
	 */
	private final org.eclipse.swt.graphics.Image add_img_32
	= PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_32x32+"Add_32x32.gif");

	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {

		super.createControls(parent, tabbedPropertySheetPage);
		final Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		browseButton = getWidgetFactory().createButton(composite, "", SWT.PUSH);
		browseButton.setImage(add_img_32);
		removeButton = getWidgetFactory().createButton(composite, "", SWT.PUSH);
		removeButton.setImage(remove_img);
		CLabel iconLabel = getWidgetFactory().createCLabel(composite, sectionLabel);

		// browseButton layout
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top  = new FormAttachment(0, ITabbedPropertyConstants.HSPACE);
		browseButton.setLayoutData(data);

		// removeButton layout
		data = new FormData();
		data.left    = new FormAttachment(browseButton, ITabbedPropertyConstants.HSPACE);
		data.top     = new FormAttachment(browseButton, 0, SWT.CENTER);
		removeButton.setLayoutData(data);

		// iconLabel layout
		data = new FormData();
		data.left  = new FormAttachment(0, 0);
		data.top   = new FormAttachment(browseButton, 0, SWT.CENTER);
		iconLabel.setLayoutData(data);

		browseButton.addMouseListener(browseButtonListener = new MouseListener() {

			public void mouseDoubleClick(MouseEvent e) {			
			}

			public void mouseDown(MouseEvent e) {				
			}

			public void mouseUp(MouseEvent e) {
				FileDialog fd = new FileDialog(composite.getShell());
				String extensions[] = {"*.jpg;*.bmp;*.ico;*.gif;*.png;*.wmf;*.emf"};
				fd.setFilterExtensions(extensions);
				String iconSelected = fd.open();

				// No image selected
				if (iconSelected == null) {
					return;
				}

				if(getElement() instanceof Stereotype) {

					File imgFile = new File(iconSelected);
					com.cea.papyrus.umlutils.Stereotype stereotype = new com.cea.papyrus.umlutils.Stereotype((Stereotype) getElement());

					try{
						stereotype.setShape(imgFile);
						refresh();
					} catch (Exception ex) {
						PapyrusMessageDialog.display("Could not import image.", null);
					}
				}
			}
		});

		removeButton.addMouseListener(removeButtonListener = new MouseListener() {

			public void mouseDoubleClick(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {
				// Default img
				if(getElement() instanceof Stereotype) {

					com.cea.papyrus.umlutils.Stereotype stereotype = new com.cea.papyrus.umlutils.Stereotype((Stereotype) getElement());

					try{
						stereotype.setShape(null);
						refresh();
					} catch (Exception ex) {
						PapyrusMessageDialog.display("Could not remove image.", null);
					}
				}
			}
		});
	}

	/**
	 * 
	 */
	@Override
	public void refresh() {
		if( !browseButton.isDisposed()){
			/* intitialization of buttons enabling */
			if(!(getElement() instanceof Stereotype)) {
				return;
			}
			com.cea.papyrus.umlutils.Stereotype stereotype = new com.cea.papyrus.umlutils.Stereotype((Stereotype) getElement());

			// Initialize icons list with null if if necessary
			if(stereotype.getUml2Stereotype().getIcons().size() < 1) {
				// Default img
				Image DEFAULT_ICON = UMLFactory.eINSTANCE.createImage();
				stereotype.getUml2Stereotype().getIcons().add(0, DEFAULT_ICON);
			}

			if(stereotype.getUml2Stereotype().getIcons().size() < 2) {
				Image DEFAULT_IMAGE = UMLFactory.eINSTANCE.createImage();
				stereotype.getUml2Stereotype().getIcons().add(1, DEFAULT_IMAGE);
			}   	

			// Get Icon
			org.eclipse.swt.graphics.Image shape = stereotype.getShape();
			// Refresh text 
			if(shape != null) {
				// Resize icon to 32x32
				org.eclipse.swt.graphics.Image resizedIcon
				= new org.eclipse.swt.graphics.Image(
						shape.getDevice()
						, shape.getImageData().scaledTo(32, 32));
				browseButton.setImage(resizedIcon);
				removeButton.setEnabled(true);

			} else {
				browseButton.setImage(add_img_32);
				removeButton.setEnabled(false);
			}   	
		}
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(browseButton!=null && !browseButton.isDisposed())
			browseButton.removeMouseListener(browseButtonListener);
		if(removeButton!=null && !removeButton.isDisposed())
			removeButton.removeMouseListener(removeButtonListener);
	}

}
